import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Anagram {

	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(System.in);

		System.out.println("Input two phrases to check if they're anagram: ");
		String phrase1 = in.nextLine();
		String phrase2 = in.nextLine();
		in.close();

		if (Anagram.isAnagram(phrase1, phrase2)) {
			System.out.printf("Phrases '%s' and '%s' are anagram", phrase1,
					phrase2);
		} else {
			System.out.println("Its not an anagram");
		}

	}

	public static boolean isAnagram(String phrase1, String phrase2) {
		char[] phrase1Sorted = sortCharsArrayOf(phrase1);
		char[] phrase2Sorted = sortCharsArrayOf(phrase2);

		if (phrase1Sorted.length != phrase2Sorted.length) {
			return false;
		}

		for (int index = 0; index < phrase1Sorted.length; ++index) {
			if (phrase1Sorted[index] != phrase2Sorted[index]) {
				return false;
			}
		}
		return true;
	}

	public static char[] sortCharsArrayOf(String phrase) {
		char[] result = null;
		result = phrase.trim().toLowerCase().toCharArray();
		Arrays.sort(result);
		return result;
	}
}
